import { Rules } from "async-validator";
import { Context } from "koa";
import mongoose, { Expression } from "mongoose";
import { userModel } from "../models/user";
import { newsInfo } from "../models/news";
import { cateInfo } from "../models/type";
import { validate } from "../utils/validate";
import { resp } from "../utils/res";
import { create, findById, findByProps } from "../utils/crud";
import { sign, verify } from "../utils/auth";
import { comparecrypt } from "../utils/bcryptjs";
import { config } from "../config/wechat";
import puppeteer, { Browser, Page } from "puppeteer";
import axios from "axios";
class Admin {
  flag: boolean;
  constructor(flag?: boolean) {
    this.flag = flag as boolean;
  }
  async index(ctx: Context) {
    //获取token
    let res = await axios.get<
      any,
      { data: { access_token: string; expires_in: number } }
    >(
      `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${config.wechat.appID}&secret=${config.wechat.appsecret}`
    );
    if (res.data.access_token) {
      ctx.state.token = res.data.access_token;
      resp.success(ctx, res.data);
    } else {
      resp.error(ctx);
    }
  }
  async register(ctx: Context) {
    const rules: Rules = {
      username: [
        {
          type: "string",
          required: true,
          message: "用户名不能为空",
        },
      ],
      password: [
        {
          type: "string",
          required: true,
          message: "密码不能为空",
        },
      ],
    };

    type LOGIN = {
      username: string;
      password: string;
    };
    const { data, error } = await validate<LOGIN>(ctx, rules);
    if (error) {
      return resp.error(ctx, error);
    }
    try {
      let findByPropsRes = await findByProps(userModel, {
        username: data.username,
      });
      if (comparecrypt(data.password, findByPropsRes.password)) {
        return resp.error(ctx, "用户名已注册!");
      } else {
        try {
          let res = await create(userModel, data);
          if (res) {
            resp.success(ctx);
          }
        } catch (error: any) {
          return resp.error(ctx, error);
        }
      }
    } catch (error: any) {
      return resp.error(ctx, error);
    }
  }
  async upload(ctx: Context) {
    let token = ctx.request.header.authorization?.split(" ")[1];
    if (!token) {
      return resp.error(ctx, "token为空");
    }
    let vres = verify(token);
    if (vres.code == 200) {
      resp.success(ctx, ctx.uploadpath);
    } else {
      resp.error(ctx, vres.data as string);
    }
  }
  async login(ctx: Context) {
    const rules: Rules = {
      username: [
        {
          type: "string",
          required: true,
          message: "用户名不能为空",
        },
      ],
      password: [
        {
          type: "string",
          required: true,
          message: "密码不能为空",
        },
      ],
    };
    type LOGIN = {
      username: string;
      password: string;
    };
    try {
      const { data, error } = await validate<LOGIN>(ctx, rules);
      if (error) {
        return resp.error(ctx, error);
      }
      try {
        let findByPropsRes = await findByProps(userModel, {
          username: data.username,
        });

        if (!findByPropsRes) {
          return resp.error(ctx, "账号错误");
        }
        if (comparecrypt(data.password, findByPropsRes.password)) {
          let signRes = sign({
            username: data.username,
            id: findByPropsRes._id,
          });
          resp.success(ctx, {
            token: signRes,
          });
        } else {
          return resp.error(ctx, "密码错误");
        }
      } catch (error: any) {
        return resp.error(ctx, error);
      }
    } catch (error: any) {
      return resp.error(ctx, error);
    }
  }

  async getNewsData(ctx: Context) {
    if (this.flag) {
      resp.success(ctx, [], "数据爬取开始，请勿过多操作");
      console.log("数据爬取开始，请勿过多操作");
      this.flag = false;
      const baseUrl = "https://news.163.com";
      (async () => {
        const options = {
          defaultViewport: {
            width: 1920,
            height: 1080,
          },
          headless: true,
        };
        const browser = await puppeteer.launch(options);
        const page = await browser.newPage();
        await page.goto(baseUrl, {
          timeout: 0,
        });
        await page.waitForSelector(".N-nav-channel");
        try {
          let allTypeRes = await page.$$eval(".N-nav-channel a", (e: any) => {
            //创建分类数据
            let arr = [];
            let nameArry = [
              "游戏",
              "直播",
              "健康",
              "辽宁",
              "汽车",
              "NBA",
              "房产",
              "家居",
              "河南",
            ];
            for (let i of e) {
              if (!nameArry.includes(i.innerText)) {
                let obj = {
                  name: i.innerText,
                  url: i.href,
                  cover: "",
                };
                arr.push(obj);
              }
            }
            return arr;
          });
          let flag = true;
          const padata = async (
            browser: Browser,
            allTypeRes: { name: string; url: string }[]
          ) => {
            if (flag) {
              flag = false;
              try {
                await gotonews(browser, allTypeRes[0].url); //新闻
                console.log("下面爬取运动数据");
              } catch (error) {
                console.log(error, "新闻");
              }
              try {
                await gotosport(browser, allTypeRes[1].url); //体育新闻
                console.log("下面爬取娱乐数据");
              } catch (error) {
                console.log(error, "运动");
              }
              try {
                await gotoyulenews(browser, allTypeRes[2].url); //娱乐新闻
                console.log("下面爬取音乐数据");
              } catch (error) {
                console.log(error, "娱乐");
              }
              try {
                await gotomusicnews(browser, allTypeRes[3].url); //音乐新闻
                console.log("下面爬取财经数据");
              } catch (error) {
                console.log(error, "音乐");
              }
              try {
                await gotocaijingnews(browser, allTypeRes[4].url); //财经新闻
                console.log("下面爬取股票数据");
              } catch (error) {
                console.log(error, "财经");
              }
              try {
                await gotogupiaonews(browser, allTypeRes[5].url); //股票新闻
                console.log("下面爬取科技数据");
              } catch (error) {
                console.log(error, "股票");
              }
              try {
                await gotokejinews(browser, allTypeRes[6].url); //科技新闻
                console.log("下面爬取手机数据");
              } catch (error) {
                console.log(error, "科技");
              }
              try {
                await gotoshoujinews(browser, allTypeRes[7].url); //手机新闻
                console.log("下面爬取智能数据");
              } catch (error) {
                console.log(error, "手机");
              }
              try {
                await gotosmartnews(browser, allTypeRes[8].url); //智能新闻
                console.log("下面爬取时尚数据");
              } catch (error) {
                console.log(error, "智能");
              }

              try {
                await gotoshishangnews(browser, allTypeRes[9].url); //时尚新闻
                console.log("下面爬取文化数据");
              } catch (error) {
                console.log(error, "时尚");
              }
              try {
                await gotowenhuanews(browser, allTypeRes[10].url); //文化新闻
                console.log("下面爬取旅游数据");
              } catch (error) {
                console.log(error, "文化");
              }
              try {
                await gotolvyounews(browser, allTypeRes[11].url); //旅游新闻
                console.log("下面爬取教育数据");
              } catch (error) {
                console.log(error, "旅游");
              }

              try {
                await gotojiaoyunews(browser, allTypeRes[12].url); //教育新闻
                console.log("下面爬取艺术数据");
              } catch (error) {
                console.log(error, "教育");
              }
              try {
                await gotoyishunews(browser, allTypeRes[13].url); //艺术新闻
                console.log("全部数据爬取完毕");
                resp.success(ctx, [], "爬取完毕");
                this.flag = true;
                await browser.close();
              } catch (error) {
                console.log(error, "艺术");
              }
            } else {
              return; //防止重复调取网页
            }
          };
          if (allTypeRes) {
            await page.close();
            for (let e of allTypeRes) {
              //创建分类列表
              cateInfo
                .create({ cate: e.name })
                .then((res: any) => {
                  console.log(`${res.cate}数据生成成功`);
                })
                .catch(async () => {
                  padata(browser, allTypeRes);
                });
            }
            padata(browser, allTypeRes);
          }
        } catch (error) {
          console.log("获取数据失败", error);
        }
      })();

      const getDetailData = async (
        browser: Browser,
        arr: { name: string; url: string; cover: string }[],
        cate: string,
        num: number = 0
      ) => {
        if (arr.length == 0) {
          return console.log("暂无数据");
        }
        return new Promise(async (resolve, reject) => {
          let this_num: number = num;
          let this_arr: { name: string; url: string; cover: string }[] = arr;
          const page = await browser.newPage();
          await page.goto(arr[this_num].url, {
            timeout: 0,
          });

          await page
            .waitForSelector(".post_title")
            .then(async () => {
              let mongooseCateId: Expression = new mongoose.Types.ObjectId(
                cate
              );
              type OBJ = {
                title: string;
                time: string;
                origin: string;
                contenthtml: string;
                cate: Expression;
                hotRate: string;
                cover: string;
              };
              let obj: OBJ = {
                title: "",
                time: "",
                origin: "",
                contenthtml: "",
                cate: mongooseCateId,
                hotRate: "",
                cover: "",
              };

              try {
                obj.title = await page.$$eval(".post_title", (e: any) => {
                  return e.map((i: any) => i.innerText)[0];
                });
              } catch (error) {
                console.log("获取标题数据失败", error);
              }
              // try {
              //   obj.url = await page.$$eval(".post_content img", (e) => {
              //     return e.map((i: any) => i.src)[0];
              //   });
              // } catch (error) {
              //   console.log("获取图片数据失败", error);
              // }
              try {
                let res = await page.$$eval(".post_info", (e) => {
                  let timea = e.map((i: any) => i.innerText)[0]?.split(" ")[0];
                  let origin = e.map((i: any) => i.innerText)[0]?.split(" ")[2];
                  let timeb = e
                    .map((i: any) => i.innerText)[0]
                    ?.split(" ")[1]
                    ?.split("来源")[0]
                    ?.replace(/[\r\n\s]/g, "");
                  let time = timea + " " + timeb;
                  return {
                    time,
                    origin,
                  };
                });
                obj.time = res.time;
                obj.origin = res.origin;
              } catch (error) {
                console.log("获取时间来源失败", error);
              }

              try {
                obj.contenthtml = await page.$$eval(".post_body", (e) => {
                  return e
                    .map((i: any) => i.innerText)[0]
                    ?.replace(/[\r\n\s]/g, "");
                });
              } catch (error) {
                console.log("获取内容失败", error);
              }

              try {
                obj.hotRate = await page.$$eval(".js-tiejoincount", (e) => {
                  return e
                    .map((i: any) => i.innerText)[0]
                    ?.replace(/[\r\n\s]/g, "");
                });
              } catch (error) {
                console.log("获取热度失败", error);
              }
              const if_obj_is_null = (obj: any) => {
                // 判断一个对象下是否有空属性
                for (const key in obj) {
                  if (obj.hasOwnProperty(key)) {
                    if (obj[key] === null || obj[key] === "") {
                      console.log("为空");
                      return false;
                    } else {
                      console.log(`${obj.title}---${num + 1}/${arr.length}`);
                      return true;
                    }
                  }
                }
              };
              if (if_obj_is_null(obj)) {
                newsInfo
                  .create({
                    title: obj.title,
                    time: obj.time,
                    origin: obj.origin,
                    contenthtml: obj.contenthtml,
                    cate: cate,
                    cover: arr[num].cover,
                    hotRate: obj.hotRate,
                  })
                  .then(() => {
                    // console.log(res);
                  })
                  .catch((err) => {
                    if (err.code == 11000) {
                      console.log("已存在此信息，爬取下一个");
                    } else {
                      console.log("mongoose数据库创建错误", err);
                    }
                  });

                await page.close();
                if (this_num < this_arr.length - 1) {
                  // if (this_num < 5) {
                  this_num++;
                  getDetailData(browser, this_arr, cate, this_num)
                    .then(() => {
                      resolve({ code: 200 });
                    })
                    .catch(() => {
                      reject({ code: 201 });
                    });
                } else {
                  resolve({ code: 200 });
                }
              }
            })
            .catch(async () => {
              console.log("此页面非爬取设定页面，跳转到下一个页面...");
              await page.close(); //解决页面不关
              this_num++;
              getDetailData(browser, this_arr, cate, this_num)
                .then(() => {
                  resolve({ code: 200 });
                })
                .catch(() => {
                  reject({ code: 201 });
                });
            });
        });
      };

      const getData: (
        page: Page,
        target?: string
      ) => Promise<{ name: string; url: string; cover: string }[]> = (
        page,
        target = ".ndi_main .news_title  a:not(.tie_count)"
      ) => {
        return new Promise<{ name: string; url: string; cover: string }[]>(
          async (resolve, reject) => {
            let ntitle = [] as any;
            let img = [] as any;
            try {
              ntitle = await page.$$eval(`${target}`, (e: any) => {
                const arr = [];
                for (let i of e) {
                  let isCode =
                    i.href
                      .split("/")
                      [i.href.split("/").length - 1].split(".")[0].length == 16;
                  if (
                    isCode &&
                    /[a-zA-Z0-9]{16}/.test(
                      i.href
                        .split("/")
                        [i.href.split("/").length - 1].split(".")[0]
                    )
                  ) {
                    let obj = {
                      name: i.innerText,
                      url: i.href,
                    };
                    arr.push(obj);
                  }
                }
                return arr;
              });
            } catch (error) {
              console.log("获取分页数据失败", error);
            }
            try {
              img = await page.$$eval(`.na_pic img`, (e: any) => {
                // const arr = [];
                // for (let i of e) {
                //   let isCode =
                //     i.href
                //       .split("/")
                //       [i.href.split("/").length - 1].split(".")[0].length == 16;
                //   if (
                //     isCode &&
                //     /[a-zA-Z0-9]{16}/.test(
                //       i.href
                //         .split("/")
                //         [i.href.split("/").length - 1].split(".")[0]
                //     )
                //   ) {
                //     let obj = {
                //       name: i.innerText,
                //       url: i.href,
                //     };
                //     arr.push(obj);
                //   }
                // }
                return e.map((i: any) => i.src);
              });
            } catch (error) {
              console.log("获取图片数据失败", error);
            }
            ntitle.forEach((e: any, i: any) => {
              e.cover = img[i];
            });
            resolve(ntitle as any);
            return ntitle;
          }
        );
      };
      //获取体育数据 637b58cfc2864dcb2353cd31
      const gotosport = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);
          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取爬取信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes: {
                      name: string;
                      url: string;
                      cover: string;
                    }[] = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "体育" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---运动数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取新闻数据 637b58cfc2864dcb2353cd30
      const gotonews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取爬取信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes: {
                      name: string;
                      url: string;
                      cover: string;
                    }[] = await getData(page);
                    console.log(`<<---爬取数据量为${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "新闻" }).then((data: any) => {
                      getDetailData(browser, getdataRes as any, data._id)
                        .then((res) => {
                          console.log("<<---新闻爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      async function autoScroll(page: Page) {
        await page.evaluate(async () => {
          await new Promise(async (resolve, reject) => {
            var totalHeight = 0;
            var distance = 100;
            var timer = setInterval(async () => {
              var scrollHeight = document.body.scrollHeight;
              window.scrollBy(0, distance);
              totalHeight += distance;
              if (totalHeight >= scrollHeight) {
                clearInterval(timer);
                console.log("到底部了");
                resolve(true);
              }
            }, 100);
          });
        });
      }

      //获取音乐新闻 637b58cfc2864dcb2353cd34
      const gotomusicnews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "音乐" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---音乐数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取娱乐新闻 637b58cfc2864dcb2353cd33
      const gotoyulenews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "娱乐" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---娱乐数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch(() => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };

      //获取财经新闻 637b58cfc2864dcb2353cd35
      const gotocaijingnews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "财经" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---财经数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch(() => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取股票新闻 637b58cfc2864dcb2353cd35
      const gotogupiaonews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);
          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "股票" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---股票数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch(() => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取科技新闻 637b58cfc2864dcb2353cd38
      const gotokejinews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          await autoScroll(page);
          let getdataRes = await getData(page);
          console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
          await page.close();
          cateInfo.findOne({ cate: "科技" }).then((data: any) => {
            getDetailData(browser, getdataRes, data._id)
              .then(() => {
                console.log("<<---科技数据爬取完毕--->>");
                resolve({ code: 200 });
              })
              .catch((err: any) => {
                reject({ code: 201 });
                console.log(err, "catch other");
              });
          });
        });
      };
      //获取手机新闻 637b58cfc2864dcb2353cd39
      const gotoshoujinews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "手机" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---手机数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err: any) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取智能数据 637b58cfc2864dcb2353cd3a
      const gotosmartnews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "智能" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---智能数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取时尚数据 637b58cfc2864dcb2353cd3b
      const gotoshishangnews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          await autoScroll(page);
          let getdataRes = await getData(page);
          console.log("<<---获取链接信息完毕--->>");
          console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
          await page.close();
          cateInfo.findOne({ cate: "时尚" }).then((data: any) => {
            getDetailData(browser, getdataRes, data._id)
              .then(() => {
                console.log("<<---时尚数据爬取完毕--->>");
                resolve({ code: 200 });
              })
              .catch((err: any) => {
                reject({ code: 201 });
                console.log(err, "catch other");
              });
          });
        });
      };
      //获取文化数据 637b58cfc2864dcb2353cd3d
      const gotowenhuanews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".datalist");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(
                      page,
                      ".datalist .news_title h3 a:not(.tie_count)"
                    );
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "文化" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then((res) => {
                          console.log("<<---文化数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //获取旅游数据 637b58cfc2864dcb2353cd3e
      const gotolvyounews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "旅游" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then((res) => {
                          console.log("<<---旅游数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //教育数据 637b58cfc2864dcb2353cd41
      const gotojiaoyunews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "教育" }).then((data: any) => {
                      if (data) {
                        getDetailData(browser, getdataRes, data._id)
                          .then(() => {
                            console.log("<<---教育数据爬取完毕--->>");
                            resolve({ code: 200 });
                          })
                          .catch((err: any) => {
                            reject({ code: 201 });
                            console.log(err, "catch other");
                          });
                      } else {
                        console.log("教育id为null或者错误");
                      }
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
      //艺术数据 637b58cfc2864dcb2353cd44
      const gotoyishunews = async (browser: Browser, url: string) => {
        return new Promise(async (resolve, reject) => {
          const page = await browser.newPage();
          await page.goto(url, {
            timeout: 0,
          });
          await page.waitForSelector(".ndi_main");
          let timer: any = null;
          await autoScroll(page);

          timer = setInterval(async () => {
            page
              .$(".post_addmore")
              .then((btn: any) => {
                btn.click().catch(async (err: any) => {
                  if (err) {
                    console.log("<<---获取链接信息完毕--->>");
                    clearInterval(timer);
                    let getdataRes = await getData(page);
                    console.log(`<<---爬取数据量为:${getdataRes.length}--->>`);
                    await page.close();
                    cateInfo.findOne({ cate: "艺术" }).then((data: any) => {
                      getDetailData(browser, getdataRes, data._id)
                        .then(() => {
                          console.log("<<---艺术数据爬取完毕--->>");
                          resolve({ code: 200 });
                        })
                        .catch((err: any) => {
                          reject({ code: 201 });
                          console.log(err, "catch other");
                        });
                    });
                  }
                });
              })
              .catch((err: any) => {
                console.log(err, "wtf");
              });
          }, 2000);
        });
      };
    } else {
      resp.success(ctx, [], "正在爬取中，请等待爬取完毕，或者重启服务器");
      console.log("正在爬取中，请等待爬取完毕，或者重启服务器");
      return;
    }
  }
}

const admin = new Admin(true);
export { admin };
